package com.coldworks.coldjoke.manager;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import android.content.Context;
import android.os.Handler;
import android.os.Message;

import org.json.JSONObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.coldworks.coldjoke.model.TopicModel;
import com.coldworks.coldjoke.task.FetchTopicsTask;
import com.coldworks.coldjoke.util.CONST;
import com.coldworks.coldjoke.util.JsonUtil;
import com.coldworks.coldjoke.util.UrlUtil;

public class TopicsManager {
	protected final Logger log = LoggerFactory.getLogger(this.getClass());
	public static final int REFRESHING_NEWER = 0;
	public static final int REFRESHING_RESET = 1;
	public static final int REFRESHING_OLDER = 2;
	public static final int REFRESH_SUCC = 3;
	public static final int REFRESH_FAIL = 4;
	public static final int REFRESH_IDLE = 5;
	private List<TopicModel> listNew;
	private List<TopicModel> listShow;
	private List<TopicModel> listNext;
	private Handler handler;
	private FetchTopicsTask fetchTopicsTask;
	private int pageStart = 0;
	private int state;
	private Map<String, TopicModel> map;
	private int limit = CONST.VALUE.TOPIC_PAGE_SIZE;

	// 不同到列别NEW出来到是不同的
	private String topicType;

	public TopicsManager(String topicType) {
		this.topicType = topicType;
		map = new HashMap<String, TopicModel>();
		listNew = new ArrayList<TopicModel>();
		listShow = new ArrayList<TopicModel>();
		listNext = new ArrayList<TopicModel>();
		state = REFRESH_IDLE;
	}

	public List<TopicModel> getListShow() {
		return listShow;
	}

	public List<TopicModel> getListNew() {
		return listNew;
	}

	public int getListNewSize() {
		return listNew.size();
	}

	public List<TopicModel> getListNext() {
		return listNext;
	}

	public void setState(int state) {
		this.state = state;
	}

	public int getState() {
		return state;
	}

	public int getPageStart() {
		return pageStart;
	}

	public void setPageStart(int mCount) {
		this.pageStart = this.pageStart + mCount;
	}

	public synchronized FetchTopicsTask getFetchJokesTask() {
		return fetchTopicsTask;
	}

	public synchronized void cancleFetchTopicTask() {
		fetchTopicsTask = null;
	}

	public synchronized void fetchTopicsTask(final Context context,
			int operation, final RefreshListCallback refreshCallback) {

		handler = new Handler() {
			public void handleMessage(Message message) {
				if (message.what == REFRESH_SUCC) {
					JSONObject jsonObject = (JSONObject) message.obj;
					if (jsonObject == null) {
						log.info("refresh failed");
						refreshCallback.refreshDone(REFRESH_FAIL);
						return;
					}
					switch (state) {
					case REFRESHING_NEWER:
						pageStart = pageStart + CONST.VALUE.TOPIC_PAGE_SIZE;
						mergeListNewer(JsonUtil.getTopicInfoList(jsonObject));
						break;
					case REFRESHING_OLDER:
						pageStart = pageStart + CONST.VALUE.TOPIC_PAGE_SIZE;
						mergeListOlder(JsonUtil.getTopicInfoList(jsonObject));
						break;
					case REFRESHING_RESET:
						resetList(JsonUtil.getTopicInfoList(jsonObject));
						pageStart = 0;
						break;
					}
					refreshCallback.refreshDone(REFRESH_SUCC);
					log.info("refresh succ");
				} else if (message.what == REFRESH_FAIL) {
					state = REFRESH_FAIL;
					log.info("refresh failed");
					refreshCallback.refreshDone(REFRESH_FAIL);
				}
			}
		};

		String topicsUrl = null;

		switch (operation) {
		case REFRESHING_RESET:
			state = REFRESHING_RESET;
			pageStart = 0;
			topicsUrl = UrlUtil.getTopicsUrl(topicType, pageStart, limit);
			log.info("refreshing reset");
			break;
		case REFRESHING_NEWER:
			state = REFRESHING_NEWER;
			topicsUrl = UrlUtil.getTopicsUrl(topicType, 0, limit);
			log.info("refreshing newer");
			break;
		case REFRESHING_OLDER:
			state = REFRESHING_OLDER;
			topicsUrl = UrlUtil.getTopicsUrl(topicType, pageStart, limit);
			log.info("refreshing older");
			break;
		}
		fetchTopicsTask = new FetchTopicsTask(context, handler, topicsUrl);
		fetchTopicsTask.start();
		log.info("start");
	}

	public void mergeListNewer(List<TopicModel> listTemp) {
		List<TopicModel> list = new ArrayList<TopicModel>();
		if (listTemp == null) {
			return;
		}
		for (TopicModel topicInfo : listTemp) {
			if (!map.containsKey(String.valueOf(topicInfo.getGatherId()))) {
				map.put(String.valueOf(topicInfo.getGatherId()), topicInfo);
				list.add(topicInfo);
			}
		}
		listNew = list;
		listShow.addAll(0, list);
	}

	public void mergeListOlder(List<TopicModel> listTemp) {
		if (listTemp == null) {
			return;
		}
		for (TopicModel topicInfo : listTemp) {
			if (!map.containsKey(String.valueOf(topicInfo.getGatherId()))) {
				map.put(String.valueOf(topicInfo.getGatherId()), topicInfo);
			}
		}
		listShow.addAll(listTemp);
	}

	public void resetList(List<TopicModel> listTemp) {
		listShow = listTemp;
		listNext.clear();
		listNew.clear();
		map.clear();
		if (listTemp == null) {
			return;
		}
		for (TopicModel topicInfo : listTemp) {
			if (!map.containsKey(String.valueOf(topicInfo.getGatherId()))) {
				map.put(String.valueOf(topicInfo.getGatherId()), topicInfo);
			}
		}
	}

}
